﻿using System;
using System.IO;
using System.Threading;
using System.Web;

namespace WebFormDemo
{
    public partial class Test : System.Web.UI.Page
    {
        //[ThreadStatic]
        private static DateTime? Now;

        private static int _Times = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Now == null)
            {
                Now = DateTime.Now;
            }

            if (HttpContext.Current.Items["Now"] == null)
            {
                HttpContext.Current.Items["Now"] = DateTime.Now;
            }

            string content = $"第{_Times++}次，线程：{Thread.CurrentThread.ManagedThreadId}," +
                        $"ThreadStatic时间：{Now}," +
                        $"HttpContext.Current.Items时间：{HttpContext.Current.Items["Now"]}。\r\n";

            DoSomething();

            File.AppendAllText(@"C:\Log.txt", content);

            Response.Write(content);
        }

        public void DoSomething()
        {
            string str = HttpContext.Current.Items["Now"].ToString();
        }
    }
}